package com.example.smartcity1.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.example.smartcity1.R;
import com.example.smartcity1.bean.ServiceBean;

import java.util.List;

import static com.example.smartcity1.network.RetrofitClient.IP;

public class ServiceAdapter extends ArrayAdapter<ServiceBean.RowsDTO> {

    private static final String TAG = "ServiceAdapter";

    public ServiceAdapter(@NonNull Context context, int resource, @NonNull List<ServiceBean.RowsDTO> objects) {
        super(context, resource, objects);
    }

//    public ServiceAdapter(Context context, List<ServiceBean.RowsDTO> mServiceList) {
//        super();
//        this.context = context;
//        this.mServiceList = mServiceList;
//    }

        @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_grid_icon,parent,false);
//            convertView = View.inflate(getContext(), R.layout.item_grid_icon,null);
            viewHolder = new ViewHolder();
            viewHolder.imageView = convertView.findViewById(R.id.item_grid_img_icon);
            viewHolder.textView = convertView.findViewById(R.id.item_grid_txt_icon);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        ServiceBean.RowsDTO rowsDTO = getItem(position);
        viewHolder.textView.setText(rowsDTO.getServiceName());
        if (rowsDTO.getImgUrl().equals("All")) {
            Glide.with(getContext()).load(R.drawable.ic_baseline_border_all_24)
                    .into(viewHolder.imageView);
        } else {
            Glide.with(getContext()).load(IP + rowsDTO.getImgUrl())
                    .placeholder(R.drawable.ic_baseline_cloud_download_24)
                    .into(viewHolder.imageView);
        }


        Log.d(TAG, "getView: " + IP + rowsDTO.getImgUrl());
        return convertView;
    }
    private class ViewHolder {
        ImageView imageView;
        TextView textView;
    }
}
